﻿using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;

namespace Super.Hmi.Converters
{
    /// <summary>
    /// 将枚举值转换为其描述属性的值
    /// </summary>
    class EnumDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            var field = value.GetType().GetField(value.ToString());
            var attribute = field?.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
